/**
 * Created by liebling on 2017/7/9.
 */
import $script from 'scriptjs';
import '../main';
import '../../css/userCenter/userManage.less';
import '../../css/parts/common.less';
import '../../../public/plugins/xui/xui.1.0.0.css';

var ApiService = require('../module/ApiService');
var UserService = require('../module/UserService');

$script.ready('jqueryFile', function () {
	$script(['//cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js'], function () {
		require('../component/common/header');
		require('../component/common/header2');
		require('../component/common/workNav');
		require('../component/common/list');
		require('../../../public/plugins/laydate/laydate');
		require('../../../public/plugins/jquery.validate.extend');
		let xui = require('../../../public/plugins/xui/xui.1.0.0');

		var index = layer.load(1, {shade: [1, '#fff'], scrollbar: false});
		let userGroupList = [];
		var Page = new function () {
			var objList = [];
			var url = decodeURIComponent(location.search);
			if(url.indexOf("?")!= -1){
				url = url.substr(1);
				var urlList = url.split('&');
				var arr = [];
				urlList.forEach(function (e) {
					var list = e.split('=');
					var obj = {};
					_.forEach(list,function (value,key) {
						switch (key){
							case 0:
								obj.name = value;
								break;
							case 1:
								obj.val = value;
								break;
						}
					});
					arr.push(obj);
				});
				objList = arr;
			}
			var obj = {};
			objList.forEach(function (e) {
				switch (e.name){
					case 'id':
						obj.id = e.val;
						break;
				}
			});
			return obj;
		}; 
		ApiService.postApi('userGroupList', {pageSize: 9999})
			.then(function (res) {
				userGroupList = res.data.results;
				xui.picker({
					'element': $('#userGroupName'),
					'value': _.map(userGroupList, function (item) {
						return {
							name: item.name,
							value: item._id,
						};
					}),
					'onClose': function (val, element, selectCol) {
						$('#userGroup').val(selectCol.data('value'));
					}
					
				});
				layer.close(index);
				return null;
			})
			.then(function () {
				//验证初始化
				;(function () {
					$.validator.addMethod('isPWOK', function (value, element) {
						var reg = /^[A-Za-z0-9]{6,16}$/;
						if (reg.test(value) || value == "") {
							return true;
						} else {
							return false;
						}
					}, '6-16位密码(字母或数字)');
					$.validator.addMethod('isPhoneOK', function (value, element) {
						var reg = /^(0\d{2,4})?-?\d{7,8}$/;
						if (reg.test(value) || value == "") {
							return true;
						} else {
							return false;
						}
					}, '座机号码非法');
					$.validator.addMethod('isEmailOK', function (value, element) {
						var reg = /^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/;
						if (reg.test(value) || value == "") {
							return true;
						} else {
							return false;
						}
					}, '邮箱非法');
					$.validator.addMethod('isMobileOk', function (value, element) {
						var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(14[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
						if (reg.test(value) || value == "") {
							return true;
						} else {
							return false;
						}
					}, '手机号非法');
					$('#createUser').validate({
						errorElement: "label",
						onkeyup: false,
						ignore: '.notCheck',
						errorPlacement: function (er, el) {
							$(el).removeClass("m-success-con");
							$(el).addClass("m-error-con");
							$(el).closest(".fn-item").find(".xui-wordError").append(er);
						},
						success: function (element, callblack) {
							$(element).remove();
							$(callblack).removeClass("m-error-con");
							$(callblack).addClass("m-success-con");
						},
						onfocusout: function (element) {
							$(element).valid();
						},
						rules: {
							project: {
								required: true
							},
							realName: {
								required: true
							},
							email: {
								required: true,
								isEmailOK: true
							},
							mobile: {
								required: true,
								number: true,
								minlength: 11,
								isMobileOk: true
							},
							phone: {
								isPhoneOK: true
							},
							password: {
								required: true
							},
							password_confirm: {
								required: true,
								equalTo: '#password'
							}
						},
						messages: {
							project: {
								required: '必填'
							},
							realName: {
								required: '必填'
							},
							email: {
								required: '必填'
							},
							mobile: {
								required: '必填',
								number: '数字',
								minlength: '11位',
							},
							phone: {
								isPhoneOK: '座机号码不正确',
							},
							password: {
								required: '必填'
							},
							password_confirm: {
								required: '必填',
								equalTo: '不相同'
							}
						},
						submitHandler: function (from) {
							var params = ApiService.serialize($(from).find('input[name]:not(.ignore),select[name]:not(.ignore)'));
							_.set(params, 'userName', params.realName);
							ApiService.postApi('update', params)
								.then(ApiService.error)
								.then(function (res) {
									layer.msg(res.message, {
										time: 2000
									}, function () {
										window.location.reload();
									})
								})
								.catch(function (err) {
								});
							return false;
						}
					});
				}());
				//修改密码
				;(function () {
					$('.f-alterPassWord').click(function () {
						var HTML = '<form class="xui-form" id="alertPassWord"><div class="xui-item"><label class="xui-lab" for="newPassWord">请输入新密码：</label><div class="xui-input-block"><input type="password" class="xui-input-text" id="newPassWord" name="newPassWord"></div><div class="xui-input-block"><div class="xui-wordError"></div></div></div><div class="xui-item"><label class="xui-lab" for="equalPassWord">请确认新密码：</label><div class="xui-input-block"><input type="password" class="xui-input-text" id="equalPassWord" name="equalPassWord"></div><div class="xui-input-block"><div class="xui-wordError"></div></div></div><div class="xui-item"><div class="xui-input-block" style="margin: 0 140px"><input type="submit" class="xui-button xui-button-red" value="保存"></div></div></form>';
						var temp = _.template(HTML);
						layer.open({
							title:'账户密码修改',
							shadeClose: true,
							type: 1,
							btn:[],
							area: ['650px', 'auto'],
							content: temp({data:{}}),
							success:function () {
								$('#alertPassWord').validate({
									errorElement: "label",
									onkeyup: false,
									ignore: '.notCheck',
									errorPlacement: function (er, el) {
										$(el).removeClass("m-success-con");
										$(el).addClass("m-error-con");
										$(el).closest(".xui-item").find(".xui-wordError").append(er);
									},
									success: function (element, callblack) {
										$(element).remove();
										$(callblack).removeClass("m-error-con");
										$(callblack).addClass("m-success-con");
									},
									onfocusout: function (element) {
										$(element).valid();
									},
									rules: {
										newPassWord: {
											required: true,
											minlength:6,
										},
										equalPassWord: {
											required: true,
											equalTo:'#newPassWord'
										}
									},
									messages: {
										newPassWord: {
											required: '必填',
											minlength:'最少6位密码',
										},
										equalPassWord: {
											required: '必填',
											equalTo:'两次输入不相同'
										}
									},
									submitHandler: function (from) {
										var params = ApiService.serialize($(from).find('input[name]:not(.ignore),select[name]:not(.ignore)'));
										_.set(params, 'id', Page.id);
										ApiService.postApi('update', params)
											.then(ApiService.error)
											.then(function (res) {
												layer.msg(res.message, {
													time: 2000
												}, function () {
													window.location.reload();
												})
												
											})
											.catch(function (err) {
												layer.msg(err);
											});
										return false;
									}
								});
							}
						})
					});
				}());
			})
	});
});
